﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TakePhotoTogether.Communication.Protocols
{
    /// <summary>
    /// Data class that represents a payload used by sync operations
    /// </summary>
    public class SyncData : ImageData
    {
        public string PhotoFileName = "";

        public SyncData(byte[] data)
            : base(data)
        {
            int stringByteSize = BitConverter.ToInt32(data, 0);

            PhotoFileName = Util.GetString(data, stringByteSize, 4);

            ParseFromBytes(data, stringByteSize + 4);
        }

        public SyncData(byte[] imageBytes, int imageWidth, int imageHeight, string filename = "")
            : base(imageBytes, imageWidth, imageHeight)
        {
            PhotoFileName = filename;
        }

        public override byte[] ToBytes()
        {
            byte[] packetData;

            byte[] imageData = base.ToBytes();
            byte[] stringData = Util.GetBytes(PhotoFileName);

            packetData = new byte[imageData.Length + stringData.Length + 4];

            byte[] stringDataSize = BitConverter.GetBytes(stringData.Length);

            for (int i = 0; i < 4; i++)
            {
                packetData[i] = stringDataSize[i];
            }

            int pos = 4;

            for (int i = 0; i < stringData.Length; i++)
            {
                packetData[pos] = stringData[i];
                pos++;
            }

            for (int i = 0; i < imageData.Length; i++)
            {
                packetData[pos] = imageData[i];
                pos++;
            }

            return packetData;
        }
    }
}
